package top.csjss.hospital.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.csjss.hospital.entity.SysMenu;
import top.csjss.hospital.service.SysMenuService;
import top.csjss.hospital.vo.LayUIDTreeVO;
import top.csjss.hospital.vo.R;
import top.csjss.hospital.vo.SysMenuVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ztf
 * @Date 20:29 2021/10/19
 **/
@RestController
public class MenuController {
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 查询左侧树结构
     * @param spread
     * @return
     */
    @PostMapping("/sel/loadMenuMangerLeftTreeJson")
    public R loadMenuMangerLeftTreeJson(Integer spread){
        // 将spread当做标识，如果传递了spread，只查询一级和二级菜单返回结果，应用于添加&修改
        //1. 调用Service查询菜单信息
        List<SysMenu> sysMenuList = sysMenuService.findAll();
        //封装数据
        List<LayUIDTreeVO> data = new ArrayList<>();
        if (spread!=null){
            for (SysMenu sysMenu : sysMenuList) {
                if (sysMenu.getParentId()==0||sysMenu.getParentId()==1){
                    LayUIDTreeVO vo = new LayUIDTreeVO();
                    BeanUtils.copyProperties(sysMenu, vo);
                    data.add(vo);
                }
            }
        }else {
            for (SysMenu sysMenu : sysMenuList) {
                LayUIDTreeVO vo =new LayUIDTreeVO();
                BeanUtils.copyProperties(sysMenu, vo);
                data.add(vo);
            }
        }
        return R.ok(data);
    }

    /**
     * 展示table数据
     * @param title
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/sel/queryMenuAllList")
    public R queryMenuAllList(String title, @RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10")Integer limit){
        //调用service
        PageInfo<SysMenu> pageInfo= sysMenuService.findByTitle(title,page,limit);
        //封装数据
        Long count = pageInfo.getTotal();
        List<SysMenuVO> data=new ArrayList();
        for (SysMenu sysMenu : pageInfo.getList()) {
            SysMenuVO vo = new SysMenuVO();
            BeanUtils.copyProperties(sysMenu, vo);
            data.add(vo);
        }
        //返回
        return R.ok(count,data);
     }
}
